package com.the4man.cnbdw.action;

import com.opensymphony.xwork2.ModelDriven;
import com.the4man.cnbdw.service.DeviceService;
import com.the4man.cnbdw.service.EmployeeService;
import com.the4man.cnbdw.service.WarnInfoHistoryService;
import com.the4man.cnbdw.utils.Page;
import com.the4man.cnbdw.utils.SelectModel;
import com.the4man.framework.action.BaseAction;
import com.the4man.framework.model.*;
import com.the4man.framework.model.gen.WarnInfoHistoryExampleBase;
import com.the4man.framework.pagemodel.PUser;
import com.the4man.framework.service.UserInfoServiceI;
import com.the4man.framework.util.DateUtil;
import com.the4man.framework.util.LOG;
import com.the4man.framework.util.PageJson;
import com.the4man.framework.util.RequestUtil;
import com.the4man.framework.utils.GenUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Action(
        value = "warnInfoHistoryAction",
        results = {@Result(
                name = "listPage",
                location = "/cnbdw/warnInfoHistory/list.jsp"
        ), @Result(
                name = "editPage",
                location = "/cnbdw/warnInfoHistory/edit.jsp"
        )}
)
public class WarnInfoHistoryAction extends BaseAction implements ModelDriven<WarnInfoHistory> {
    private WarnInfoHistory warnInfo = new WarnInfoHistory();
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private WarnInfoHistoryService warnInfoHistoryService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    UserInfoServiceI userInfoService;

    public WarnInfoHistoryAction() {
    }

    public String indexPage() {
        return "listPage";
    }

    public String toEditPage() {
        try {
            WarnInfo d = (WarnInfo)this.warnInfoHistoryService.selectByPrimaryKey(this.getRequest().getParameter("id"), WarnInfo.class);
            BeanUtils.copyProperties(d, this.warnInfo);
        } catch (Exception var2) {
            var2.printStackTrace();
        }

        return "editPage";
    }

    public void getAllDatasourceList() {
        try {
            WarnInfoHistoryExample warnInfoHistoryExample = new WarnInfoHistoryExample();
            WarnInfoHistoryExampleBase.Criteria cc = warnInfoHistoryExample.createCriteria();
            if (StringUtils.isNotEmpty(this.getRequest().getParameter("empName"))) {
                cc.andEmpNameEqualTo(this.getRequest().getParameter("empName"));
            }

            if (StringUtils.isNotEmpty(this.getRequest().getParameter("start"))) {
                cc.andCreateTimeGreaterThanOrEqualTo(this.getRequest().getParameter("start"));
            }

            if (StringUtils.isNotEmpty(this.getRequest().getParameter("end"))) {
                cc.andCreateTimeLessThanOrEqualTo(this.getRequest().getParameter("end"));
            }

            this.warnInfoHistoryService.countByExample(warnInfoHistoryExample);
            Page<WarnInfoHistory> page = new Page();
            this.setPageProperties(page);
            this.warnInfoHistoryService.selectByExample(warnInfoHistoryExample, page);
            DataGrid d = new DataGrid();
            d.setRows(page.getData());
            d.setTotal((long)page.getRowCount());
            this.writeJson(d);
        } catch (Exception var6) {
            LOG.error("获取数据失败", var6, new String[0]);
        }

    }

    public void getWarnCount() {
        try {
            int total = this.warnInfoHistoryService.countByWarn();
            this.writeJson(total);
        } catch (Exception var2) {
            ;
        }

    }

    public void update() {
        PageJson json = new PageJson();
        PageJson pageJson = new PageJson();
        PUser puser = new PUser();

        try {
            HttpServletRequest request = this.getRequest();
            String id = request.getParameter("id");
            String password = request.getParameter("password");
            String username = RequestUtil.getSessionUserinfo().getUsername();
            puser.setLoginType("DQM");
            puser.setUserpwd(password);
            puser.setUserid(username);
            this.userInfoService.validateDqmUser(pageJson, puser);
            if (pageJson.isReturnFlag()) {
                String handle = request.getParameter("handle");
                this.warnInfo.setId(id);
                this.warnInfo.setHandle(handle);
                int i = this.warnInfoHistoryService.handleWarn(this.warnInfo);
                json.setReturnFlag(true);
                json.setReturnMsg("处理成功！");
            } else {
                json.setReturnFlag(false);
                json.setReturnMsg("密令错误！");
            }
        } catch (Exception var10) {
            json.setReturnFlag(false);
            json.setReturnMsg("处理失败");
            LOG.error("获取数据失败", var10, new String[0]);
        }

        this.writeJson(json);
    }

    public void delete() {
        PageJson json = new PageJson();

        try {
            int i = this.warnInfoHistoryService.deleteByPrimaryKey(this.getRequest().getParameter("ids"), WarnInfoHistory.class);
            if (i > 0) {
                json.setReturnMsg("删除成功！");
                json.setReturnFlag(true);
            } else {
                json.setReturnMsg("删除失败！");
                json.setReturnFlag(false);
            }
        } catch (Exception var3) {
            LOG.error("批量删除失败！", var3, new String[0]);
            json.setReturnMsg("删除失败！");
            json.setReturnFlag(false);
        }

        this.writeJson(json);
    }

    public void insertEmergencyAlarm() {
        PageJson json = new PageJson();
        String mac = this.getRequest().getParameter("mac");
        String sos = this.getRequest().getParameter("sos");

        try {
            List<com.the4man.framework.model.Employee> list = this.employeeService.selectEmployeeByMac(mac);
            String time = DateUtil.getInstance().formatDate(new Date());
            WarnInfoHistory warn = new WarnInfoHistory();
            warn.setId(GenUtil.genUUID());
            warn.setDeviceMac(mac);
            warn.setEmpName(((Employee)list.get(0)).getName());
            warn.setCreateTime(time);
            warn.setUrgentAlarm(sos);
            warn.setEmpRegion(((Employee)list.get(0)).getRegionId());
            warn.setWarnType("紧急报警");
            warn.setHandle("1");
            this.warnInfoHistoryService.insert(warn);
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        json.setReturnMsg("成功---！！！");
        json.setReturnFlag(true);
        this.writeJson(json);
    }

    public void getHandletype() {
        try {
            List<SelectModel> list = new ArrayList();
            list.add(new SelectModel("0", "已消缺"));
            list.add(new SelectModel("1", "报警"));
            list.add(new SelectModel("2", "已确认"));
            this.writeJson(list);
        } catch (Exception var2) {
            LOG.error("获取数据失败", var2, new String[0]);
        }

    }

    public WarnInfoHistory getModel() {
        return this.warnInfo;
    }
}
